package com.sky.mapper;

import com.sky.dto.*;
import com.sky.entity.Orders;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.OrderStatisticsVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrdersMapper {
    /**
     * 插入提交的用户订单
     * @param orders
     */
    void insert(Orders orders);


    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);


    /**
     *  条件查询订单
     * @param ordersPageQueryDTO
     * @return List
     */
    List<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     *  根据id查询订单信息
     * @param id
     * @return
     */
    @Select("select * from  orders where id=#{id}")
    Orders getById(Long id);

    /**
     *
     * @return
     */
    @Select("SELECT \n" +
            "  SUM(CASE WHEN `status` = 3 THEN 1 ELSE 0 END) AS `confirmed`,\n" +
            "  SUM(CASE WHEN `status` = 4 THEN 1 ELSE 0 END) AS `deliveryInProgress`,\n" +
            "  SUM(CASE WHEN `status` = 2 THEN 1 ELSE 0 END) AS `toBeConfirmed`\n" +
            "FROM `orders`;")
    OrderStatisticsVO selectStatistics();

    /**
     *  获取指定状态的订单信息
     * @param status
     * @param time
     * @return
     */
    @Select("select * from orders where status=#{status} and order_time < #{time}")
    List<Orders> getStatusAndOrderTimeLTIMT(Integer status, LocalDateTime time);

    /**
     *  根据日期查询订单列表
     * @param begin
     * @param end
     * @return
     */
    @Select("SELECT * \n" +
            "FROM orders \n" +
            "WHERE `status` = 5 \n" +
            "  AND order_time BETWEEN #{begin} AND #{end};")
    List<Orders> turnoverStatistics(String begin,String end);



    /**
     * 根据日期查询订单列表
     * @param begin
     * @param end
     * @return
     */
    @Select("SELECT * \n" +
            "FROM orders \n" +
            "where  order_time BETWEEN #{begin} AND #{end};")
    List<Orders> getByOrderTime(String begin, String end);

    /**
     * 查询dto
     * @param begin
     * @param end
     * @return
     */
    List<OrderDTO> getOrdersDTOByOrderTime(String begin, String end);
    /**
     * 根据日期查询订单列表 top10
     * @param begin
     * @param end
     * @return
     */
    List<GoodsSalesDTO> getTopTen(String begin, String end);


    /**
     * 根据日期统计金额
     * @param begin
     * @param end
     * @return
     */
    List<Double> getTurnoverByOrderTime(String begin, String end);

    /**
     * 根据日期 统计   成功的订单数
     * @param today
     * @param nextDay
     * @return
     */
    @Select("SELECT COUNT(user_id)\n" +
            "FROM orders\n" +
            "WHERE order_time BETWEEN #{today} AND #{nextDay}\n" +
            "  AND status = 5")
    Integer getUserTotalByOrderTime(String today, String nextDay);

}
